﻿(function () {
    var controllerId = "loginController";
    var app = angular.module('app', ["ngCookies"]);
    appFactory(app);
    app.controller(controllerId,
        function ($scope, method, $cookieStore) {
            var vm = $scope;
            //逻辑方法
            vm.logical = {
                //页面初始化  获取cook的值
                chushihanshu: function () {
                    var cookcsh = $cookieStore.get("zj2mobilename");
                    if (cookcsh != null) {
                        cookcsh = unescape(cookcsh);
                        vm.name = cookcsh.split("|")[0];
                        vm.paw = cookcsh.split("|")[1];
//                        vm.logical.dengru();
                    }
                },
                //按键监控
                myKeyup: function (e) {
                    var key = window.event ? e.keyCode : e.which;
                    if ($scope.name != null && $scope.paw != null) {
                        //回车键监测事件
                        if (key === 13) {
                            vm.logical.dengru();
                        }
                    }
                },
                //登入方法请求
                dengru: function () {
                    if (vm.name != null && vm.paw != null) {
                        layer.open({
                            type: 2,
                            content: "登录中"
                        });
                        //调用登入请求数据
                        vm.getData.login(vm.name, vm.paw).then(function (data) {
                            if (data !== "error") {
                                if ($("#zidongdengru").hasClass("be_checked")) {
                                    var jmpass = escape(vm.name + "|" + vm.paw);
                                    $cookieStore.put("zj2mobilename", jmpass);
                                } else {
                                    $cookieStore.remove("zj2mobilename");
                                }
                                window.location.href = "Projects.html";
                            } else {
                                alert("账号或密码有误!");
                                vm.paw = "";
                            }
                            layer.closeAll();
                        });

                    } else {
                        alert("账号或密码为空!");
                        vm.paw = "";
                    }
                }
            };
            //数据处理方法
            vm.getData = {
                //登入请求方法
                login: function () {
                    var url = "Application/login.ashx?type=login";
                    var data = { user: vm.name, paw: vm.paw };
                    //调用公共请求数据方法
                    return method.htmlAjax.post(url, data);
                }
            }
            vm.logical.chushihanshu();
        });
    
})()